package tw.com.diray;

import tw.com.diary.api.domain.CalendarDTO;
import tw.com.diary.api.service.CalendarService;
import tw.com.diary.core.CalendarServiceImpl;
import tw.com.diray.comm.ui.DatePickerListenter;
import tw.com.diray.comm.ui.DirDateBean;
import tw.com.diray.comm.ui.DirDatePciker;
import tw.com.diray.comm.ui.DirDatePickerDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * 寫日記畫面
 * 
 * @author Andy
 * 
 */
public class CalendarWriteActivity extends Activity {

	private static final String TAG = CalendarWriteActivity.class.getName();

	static final int REQUEST_IMAGE_CAPTURE = 1;

	private CalendarDTO dto = new CalendarDTO();
	private CalendarService service = new CalendarServiceImpl();

	private Button yyymmdBtn;
	private DirDatePickerDialog dirDatePickerDialog;
	private EditText content;
	private ImageView mImageView;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
			Bundle extras = data.getExtras();
			Bitmap imageBitmap = (Bitmap) extras.get("data");

			this.mImageView.setImageBitmap(imageBitmap);
			this.mImageView.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 清除內容
	 * 
	 * @param view
	 */
	public void cleanText(View view) {
		final Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(this.getString(R.string.decideTOClean));
		builder.setNegativeButton(getString(R.string.sumit), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				content.setText("");

			}
		});
		builder.setTitle(getTitle());
		builder.setMessage(getString(R.string.cleanContent));
		builder.setNeutralButton(getString(R.string.cancel), null);
		builder.create().show();
	}

	/**
	 * 送出
	 * 
	 * @param view
	 */
	public void doSumit(View view) {

		this.dto.setContent(this.content.getText().toString());

		this.service.writeBook(dto, this);
		this.setResult(RESULT_OK);
		this.finish();
	}

	@Override
	public void onBackPressed() {

		this.dto.setContent(this.content.getText().toString());

		if (TextUtils.equals(dto.getContent(), dto.getOldContent())) {
			super.onBackPressed();
		} else {
			final Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(this.getTitle());
			builder.setMessage(getString(R.string.contentChange));
			builder.setNegativeButton(getString(R.string.sumit), new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					doSumit(null);

				}
			});

			builder.setNeutralButton(getString(R.string.cancel), new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

					dto.setContent(content.getText().toString());
					dto.setOldContent(content.getText().toString());
					// TODO Auto-generated method stub

				}
			});
			builder.create().show();
		}

		// TODO Auto-generated method stub

	}

	/**
	 * 開啟時間對話框
	 * 
	 * @param view
	 */
	public void openDateDiaglog(View view) {
		this.dirDatePickerDialog.show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		Log.d(TAG, "oncreate");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calendar_write);

		this.getActionBar().setDisplayHomeAsUpEnabled(true);

		final Bundle budle = getIntent().getExtras();

		DirDateBean bean = new DirDateBean();
		if (budle == null) {
			bean = new DirDateBean();
		} else {
			final String yyyymmdd = budle.getString("yyyymmdd");

			bean = new DirDateBean(yyyymmdd);
		}

		this.yyymmdBtn = (Button) this.findViewById(R.id.yyymmddBtn);
		this.content = (EditText) this.findViewById(R.id.content);
		this.mImageView = (ImageView) this.findViewById(R.id.imageView1);

		this.mImageView.setVisibility(View.GONE);
		this.dto.setYyyymmdd(bean.getDate());
		this.yyymmdBtn.setText(bean.getShowDate());

		this.service.inti(dto, this);

		// 內容
		this.content.setText(dto.getContent());

		// // ====選擇日期
		this.dirDatePickerDialog = DirDatePciker.builder(this, "", new DatePickerListenter() {

			@Override
			public void callback(final DirDateBean bean) {

				dto.setYyyymmdd(bean.getDate());// 修改日期
				yyymmdBtn.setText(bean.getShowDate());// 畫面上顯示日期也要修改

				service.inti(dto, CalendarWriteActivity.this);// 查詢
				content.setText(dto.getContent());// 內容

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.dairy, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		final int id = item.getItemId();

		switch (id) {
		case R.id.action_save:
			this.doSumit(null);
			break;

		case R.id.action_clean:
			this.cleanText(null);
			break;

		case R.id.action_take_picture:

			Intent takePictureIntent = new Intent();
			takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

			if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
				startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
			}

			break;
		}

		return super.onOptionsItemSelected(item);
	}
}
